package com.starnet.studentmanagement.handler;

import com.starnet.studentmanagement.vo.Result;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/***
 * 全局异常统一处理类
 */
@RestControllerAdvice
public class ExceptionControllerAdvice {
    //参数不合法异常
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        return Result.badParam(objectError.getDefaultMessage());
    }
    @ExceptionHandler(ApiException.class)
    public Result ApiExceptionHandler(ApiException e){
        return new Result(e.getCode(),e.getMsg(),null);
    }
}
